package org.lmind.arena.srd35.combat;

import java.util.ArrayList;

import org.lmind.arena.srd35.Creature;
import org.lmind.arena.srd35.rule.BasicRule;

/**
 * 表示一场战斗
 * @author 刘勉
 *
 */
public class Combat {

	private BasicRule rule;

	/**
	 * 参与战斗的单位
	 */
	private ArrayList<CombatantImpl> combatants;
	
	/** 战斗顺序 */
	private ArrayList<CombatantImpl> order;
	
	private int currentAction;
	
	public Combat(BasicRule rule) {
		this.rule = rule;
	}
	
	public void initiative() {
		order = initiativeCheck();
	}
	
	/**
	 * 进行initiativeCheck
	 * @return
	 */
	public ArrayList<CombatantImpl> initiativeCheck() {
		
		ArrayList<CombatantImpl> list = new ArrayList<CombatantImpl>();
		
		// 做一个标志，方便之后排序
		CombatantImpl flag = new CombatantImpl();
		flag.initiativeCheck = -1;
		list.add(flag);
		
		for (CombatantImpl unit : combatants) {
			
			unit.initiativeCheck = rule.initiativeCheck(unit.creature);
			
			for (int i = 0; i < list.size(); i++) {
				CombatantImpl t = list.get(i);
				// initiativeCheck较高者在前
				if (unit.initiativeCheck == t.initiativeCheck) {
					// 如果initiativeCheck相同，则modifier最高者优先，如果仍然相同，则两者之间循环进行InitiativeCheck直到不同
					if (rule.initiativeModifier(unit.creature) == rule.initiativeModifier(t.creature)) {
						if (initiativeCheck(unit, t)) {
							list.add(i, unit);
						} else {
							list.add(i + 1, unit);
						}
						break;
					} else if (rule.initiativeModifier(unit.creature) > rule.initiativeModifier(t.creature)) {
						list.add(i, unit);
						break;
					} else if (rule.initiativeModifier(unit.creature) < rule.initiativeModifier(t.creature)) {
						list.add(i + 1, unit);
						break;
					}
				} else if (unit.initiativeCheck < t.initiativeCheck) {
					list.add(i, unit);
					break;
				}
			}
		}
		list.remove(list.size() - 1);
		return list;
	}
	
	public boolean hasNextCombatant() {
		if (currentAction >= order.size())
			return false;
		return true;
	}
	
	public Combatant nextCombatant() {
		CombatantImpl combatant = order.get(currentAction);
		currentAction++;
		return combatant;
	}
	
	public void nextRound() {
		if (order == null) {
			initiative();
		}
		currentAction = 0;
	}

	
	/**
	 * 循环进行initiativeCheck直到分出胜负
	 * @param a
	 * @param b
	 * @return
	 */
	private boolean initiativeCheck(CombatantImpl a, CombatantImpl b) {
		int t1 = 0, t2 = 0;
		while (t1 != t2) {
			t1 = rule.initiativeModifier(a.creature);
			t2 = rule.initiativeModifier(b.creature);
		}
		a.initiativeCheck = t1;
		b.initiativeCheck = t2;
		return t1 > t2;
	}

	
	public class CombatantImpl implements Combatant {
		
		private Creature creature;
		
		/** 措手不及状态 */
		private boolean flatFooted = true;
		
		private int initiativeCheck;

		public Creature getCreature() {
			return creature;
		}

		public boolean isFlatFooted() {
			return flatFooted;
		}

		public int getInitiativeCheck() {
			return initiativeCheck;
		}

		public void action(CombatantAction action, Object target) {
			if (!action.actived(this))
				throw new IllegalStateException("");
			action.action(this, target);
		}

	}
}
